//
//  XKAboutUsController.m
//  XKJF
//
//  Created by xyg on 2016/12/17.
//  Copyright © 2016年 xiaokang100. All rights reserved.
//

#import "XKAboutUsController.h"
#import "XKMineAboutUsHeaderCell.h"
#import "XKMineAboutUsCell.h"
#import "XKSupportController.h"
//#import <WXApi.h>



static NSString *XKMineAboutUsHeaderCellID  = @"XKMineAboutUsHeaderCellReusableIdentifier";
static NSString *XKMineAboutUsCellID        = @"XKMineAboutUsCellReusableIdentifier";

@interface XKAboutUsController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *textArray;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *showView;
@property (nonatomic, strong) UIView *bgView;
@end

@implementation XKAboutUsController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubviews];
    
    [MobClick event:@"XKAboutUsController"];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark - setupSubviews
- (void)setupSubviews {
    
    self.navigationItem.title = @"关于我们";
    self.tableView.bounces = YES;
    self.tableView.scrollEnabled = YES;
    self.tableView.frame = self.view.bounds;
    [self.view addSubview:self.tableView];
    
    self.titleArray = @[@"官方网站",
                        @"商城网站",
                        @"官方微博",
                        @"微信公众号",
                        @"客服电话",
                        @"客服QQ",
                        @"去评分"];
    self.textArray = @[@"http://www.xiaokang100.cn",
                       @"https://www.xiaokang100.com",
                       @"http://weibo.com/xiaokang100",
                       @"wechat://xiaokang100pingtai",
                       @"tel:4006677712",
                       @"mqq://im/chat?chat_type=wpa&uin=2852379850&version=1&src_type=web",
                       @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=1210615947&pageNumber=0&sortOrdering=2&mt=8"];
    
    
    UIView *bottomView = [UIView new];
    bottomView.frame = CGRectMake(0, 0, Main_Width, 80);
    
    UILabel *copyrightLabel = [UILabel getLabWithText:@"Copyright © 2017 小康100. All Rights Reserved" textFont:Font_12 textColor:GrayColor textAlignment:NSTextAlignmentCenter];
    [bottomView addSubview:copyrightLabel];
    [copyrightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(bottomView).offset(-20*Scale_H);
        make.centerX.mas_equalTo(bottomView);
    }];
    
    
    NSString *app_Version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString *currentVearsion = [NSString stringWithFormat:@"当前版本: %@",app_Version];
    UILabel *appVersionLabel = [UILabel getLabWithText:currentVearsion textFont:Font_12 textColor:GrayColor textAlignment:NSTextAlignmentCenter];
    [bottomView addSubview:appVersionLabel];
    [appVersionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(copyrightLabel).offset(-20*Scale_H);
        make.centerX.mas_equalTo(bottomView);
    }];
    
    self.tableView.tableFooterView = bottomView;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}


#pragma mark - 配置tableView
- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    }
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _tableView.separatorColor = kBaseGrayColor;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    _tableView.estimatedRowHeight = 200;
    _tableView.tableFooterView = [UIView new];
    _tableView.backgroundColor = kBaseGrayColor;
    
    [_tableView registerClass:[XKMineAboutUsHeaderCell class] forCellReuseIdentifier:XKMineAboutUsHeaderCellID];
    [_tableView registerClass:[XKMineAboutUsCell class] forCellReuseIdentifier:XKMineAboutUsCellID];
    
    
    return _tableView;
}

#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return self.titleArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        XKMineAboutUsHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:XKMineAboutUsHeaderCellID];
        if (cell == nil) {
            cell = [[XKMineAboutUsHeaderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XKMineAboutUsHeaderCellID];
        }
        return cell;
    } else {
        XKMineAboutUsCell *cell = [tableView dequeueReusableCellWithIdentifier:XKMineAboutUsCellID];
        if (cell == nil) {
            cell = [[XKMineAboutUsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:XKMineAboutUsCellID];
        }
        cell.titleString = self.titleArray[indexPath.row];
        return cell;
    }
}



#pragma mark - 代理方法
#pragma mark - cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) {
        
        if (indexPath.row == 3) {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = @"小康100";
            
            UIView *showView = [[NSBundle mainBundle] loadNibNamed:@"XKAboutUsShowView" owner:self options:nil].lastObject;
            
            CAGradientLayer *gradientLayer = [CAGradientLayer layer];
            gradientLayer.colors = @[(__bridge id)kBaseRedColor.CGColor,
                                     (__bridge id)[UIColor yellowColor].CGColor,
                                     ];
            gradientLayer.locations = @[@0.3, @1.0];
            gradientLayer.startPoint = CGPointMake(0, 0);
            gradientLayer.endPoint = CGPointMake(1.0, 1.0);
            gradientLayer.frame = CGRectMake(0, 0, 300, 380);
            [showView.layer insertSublayer:gradientLayer atIndex:0];
            
            
            showView.frame = CGRectMake((Main_Width-300)/2, (Main_Height-380)/2, 300, 380);
            showView.layer.cornerRadius = 6;
            showView.layer.masksToBounds = YES;
            showView.transform = CGAffineTransformScale(showView.transform, 0.8, 0.8);
            showView.alpha = 0;
            _showView = showView;
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            UIView *bgView = [UIView new];
            bgView.frame = Main_Bounds;
            bgView.alpha = 0;
            bgView.backgroundColor = BlackColor;
            _bgView = bgView;
            [window addSubview:bgView];
            [window addSubview:showView];
            [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:50 options:UIViewAnimationOptionCurveEaseIn animations:^{
                bgView.alpha = 0.8;
                showView.alpha = 1;
                showView.transform = CGAffineTransformIdentity;
            } completion:^(BOOL finished) {
                
            }];
            
        } else {
            
            if (SYSTEM_VERSION_LESS_THAN(@"10")) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.textArray[indexPath.row]]];
            } else {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.textArray[indexPath.row]] options:@{} completionHandler:nil];
            }
        }
    }
}


- (IBAction)cancelButtonEvent:(UIButton *)sender {
    [_showView removeFromSuperview];
    [_bgView removeFromSuperview];
}

- (IBAction)confirmButtonEvent:(UIButton *)sender {
    [_showView removeFromSuperview];
    [_bgView removeFromSuperview];
    if (SYSTEM_VERSION_LESS_THAN(@"10")) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"wechat://xiaokang100pingtai"]];
    } else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"wechat://xiaokang100pingtai"] options:@{} completionHandler:nil];
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
